package com.hilaver.dzmis.web.servlet.product;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiAccessoryOthers;
import com.hilaver.dzmis.product.ProductAccessoryHang;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.product.ProductOrderItem;
import com.hilaver.dzmis.service.impl.ProductAccessoryHangServiceImpl;

/**
 * Servlet implementation class for Servlet: OrderServlet
 * 
 */
public class ProductAccessoryHangServlet extends
		com.hilaver.dzmis.web.servlet.AbstractBaseServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	private ProductAccessoryHangServiceImpl phService;
	
	private ProductAccessoryHang accessoryHang;

	public ProductAccessoryHangServlet() {
		super();
		this.simpleName = ProductAccessoryHang.class.getSimpleName();
		this.fullName = ProductAccessoryHang.class.getName();
		this.phService = new ProductAccessoryHangServiceImpl();
		this.phService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			String outputStr = execute(request);
			if (this.action == ACTION_EDIT) {
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);
		this.phService.setLocale((Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE));
		super.setAcUser(request, phService);
		String rtn = "";

		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";

			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			rtn = this.phService.edit(this.accessoryHang);
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			rtn = this.phService.delete(this.id);
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			rtn = this.phService.get(this.id);
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"

			break;
		case ACTION_GET_ALL:
			this.setId(request);
			rtn = this.phService.getAll(this.id);
			break;
		}

		return rtn;
	}

	protected void fillObj(HttpServletRequest request) throws Exception {
		super.fillObj(request);
		this.accessoryHang = (ProductAccessoryHang)this.obj;
		
		
		ProductIdentification pi = new ProductIdentification();
		pi.setId(Integer.parseInt((String)this.getValue(request, "piId")));
		this.accessoryHang.setProductIdentification(pi);
		
		if(this.getValue(request,"aoId1") != null){
			BiAccessoryOthers biAccessoryOthers1 = new BiAccessoryOthers();
			biAccessoryOthers1.setId(Integer.parseInt((String)this.getValue(request,"aoId1")));
			this.accessoryHang.setBiAccessoryOthers1(biAccessoryOthers1);
		}
		
		if(!"".equals((String)this.getValue(request,"aoId2")) && this.getValue(request,"aoId2") != null){
			BiAccessoryOthers biAccessoryOthers2 = new BiAccessoryOthers();
			biAccessoryOthers2.setId(Integer.parseInt((String)this.getValue(request,"aoId2")));
			this.accessoryHang.setBiAccessoryOthers2(biAccessoryOthers2);
		}
		
		if(!"".equals((String)this.getValue(request,"aoId3")) && this.getValue(request,"aoId3") != null){
			BiAccessoryOthers biAccessoryOthers3 = new BiAccessoryOthers();
			biAccessoryOthers3.setId(Integer.parseInt((String)this.getValue(request,"aoId3")));
			this.accessoryHang.setBiAccessoryOthers3(biAccessoryOthers3);
		}
		
		if(!"".equals((String)this.getValue(request,"aoId4")) && this.getValue(request,"aoId4") != null){
			BiAccessoryOthers biAccessoryOthers4 = new BiAccessoryOthers();
			biAccessoryOthers4.setId(Integer.parseInt((String)this.getValue(request,"aoId4")));
			this.accessoryHang.setBiAccessoryOthers4(biAccessoryOthers4);
		}
		
		if(!"".equals((String)this.getValue(request,"aoId5")) && this.getValue(request,"aoId5") != null){
			BiAccessoryOthers biAccessoryOthers5 = new BiAccessoryOthers();
			biAccessoryOthers5.setId(Integer.parseInt((String)this.getValue(request,"aoId5")));
			this.accessoryHang.setBiAccessoryOthers5(biAccessoryOthers5);
		}
		
		if(!"".equals((String)this.getValue(request,"aoId6")) && this.getValue(request,"aoId6") != null){
			BiAccessoryOthers biAccessoryOthers6 = new BiAccessoryOthers();
			biAccessoryOthers6.setId(Integer.parseInt((String)this.getValue(request,"aoId6")));
			this.accessoryHang.setBiAccessoryOthers6(biAccessoryOthers6);
		}
		
	}	
}